package cfca.paperless.client.servlet._3_assist._3_09_GetTemplate;

import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.StringUtil;

/**
 * @Description 获取模板接口，使用示例。
 * 
 * @Author dell
 * @Date 2016-5-16
 * @CodeReviewer TODO
 */
public class GetTemplateTest01 {

    public static void main(String[] args) throws Exception {

        long timeStart = System.currentTimeMillis();// 开始时间

        System.out.println("GetTemplateTest01 START");

        PaperlessClient clientAssistBean = new PaperlessClient(PaperlessConfig.assistUrl, 10000, 60000);// 无纸化辅助接口的访问URL

        String targetTemplateCode = "T001";// 模板编码，多个模板号用逗号分隔，比如："tmp001,tmp002"
        
        String hashAlg = "md5";// hash算法，支持md5/sha1/sha256，可以为空
        
        String result = clientAssistBean.getTemplate(targetTemplateCode, hashAlg, PaperlessConfig.operatorCode);
        
        String code = StringUtil.getNodeText(result, "Code");

        // 处理结果为正常，保存生成的图片文件到本地目录下
        if ("200".equals(code)) {
            System.out.println("GetTemplateTest01 END OK");
            
            Object[] objArr = StringUtil.getXmlFieldArr(result, "Template");
            
            for(Object obj : objArr) {
                String templateCode = StringUtil.getXmlField(obj.toString(), "TemplateCode");
                System.out.println("TemplateCode:"+templateCode);
                
                String version = StringUtil.getXmlField(obj.toString(), "Version");
                System.out.println("Version:" + version);
                
                String templateName = StringUtil.getXmlField(obj.toString(), "TemplateName");
                System.out.println("TemplateName:"+ templateName);
                
                String templateData = StringUtil.getXmlField(obj.toString(), "TemplateData");
                System.out.println("TemplateData:"+ templateData.length());
                
                String TemplateFormat = StringUtil.getXmlField(obj.toString(), "TemplateFormat");
                System.out.println("TemplateFormat:"+ TemplateFormat);
                
                byte[] tmp = Base64.decode(templateData);
                IoUtil.write("./TestData/output/GetTemplateTest01_" + templateName + "." + TemplateFormat, tmp);
                
                if(StringUtil.isNotEmpty(hashAlg)){
                    String hashValue = StringUtil.getXmlField(obj.toString(), "HashValue");
                    System.out.println("HashValue:" + hashValue);
                }
               
            }
        } else {
            // 处理结果为异常，打印出错误码和错误信息
            System.out.println("GetTemplateTest01 END NG");
            System.out.println(result);
        }

        long timeEnd = System.currentTimeMillis();// 结束时间
        System.out.println("##########" + "time used:" + (timeEnd - timeStart) + "##########");

    }
}
